<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="订单编号" prop="orderId">
        <el-input
            v-model="queryParams.orderId"
            placeholder="请输入订单ID"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="服务内容" prop="serviceName">
        <el-input
            v-model="queryParams.serviceName"
            placeholder="请输入服务内容"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="评价时间" style="width: 308px">
        <el-date-picker
            v-model="daterangeReviewTime"
            value-format="YYYY-MM-DD HH:mm:ss"
            format="YYYY-MM-DD HH:mm:ss"
            type="datetimerange"
            range-separator="-"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="评价星级" prop="rating">
        <el-select style="width: 200px;" v-model="queryParams.rating" placeholder="请选择评价星级" clearable>
          <el-option
              v-for="dict in sys_review_star"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!--<el-col :span="1.5">-->
      <!--  <el-button-->
      <!--    type="primary"-->
      <!--    plain-->
      <!--    icon="Plus"-->
      <!--    @click="handleAdd"-->
      <!--    v-hasPermi="['orderManagement:reviewManagement:add']"-->
      <!--  >新增</el-button>-->
      <!--</el-col>-->
      <!--<el-col :span="1.5">-->
      <!--  <el-button-->
      <!--    type="success"-->
      <!--    plain-->
      <!--    icon="Edit"-->
      <!--    :disabled="single"-->
      <!--    @click="handleUpdate"-->
      <!--    v-hasPermi="['orderManagement:reviewManagement:edit']"-->
      <!--  >修改</el-button>-->
      <!--</el-col>-->
      <!--<el-col :span="1.5">-->
      <!--  <el-button-->
      <!--    type="danger"-->
      <!--    plain-->
      <!--    icon="Delete"-->
      <!--    :disabled="multiple"-->
      <!--    @click="handleDelete"-->
      <!--    v-hasPermi="['orderManagement:reviewManagement:remove']"-->
      <!--  >删除</el-button>-->
      <!--</el-col>-->
      <!--<el-col :span="1.5">-->
      <!--  <el-button-->
      <!--    type="warning"-->
      <!--    plain-->
      <!--    icon="Download"-->
      <!--    @click="handleExport"-->
      <!--    v-hasPermi="['orderManagement:reviewManagement:export']"-->
      <!--  >导出</el-button>-->
      <!--</el-col>-->
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="reviewManagementList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="订单编号" align="center" prop="orderId"/>
      <!--<el-table-column min-width="150" label="服务内容" align="center" prop="serviceName"/>-->
      <el-table-column min-width="150" label="服务内容" align="center" prop="serviceItem">
        <template #default="scope">
          <div v-for="(service, index) in scope.row.serviceItem" :key="index">
            {{ service.serviceName }}
          </div>
        </template>
      </el-table-column>
      <el-table-column label="评价人" align="center" prop="member">
        <template #default="scope">
          <span>{{ scope.row.member.memberName }}</span>
          <span>{{ scope.row.member.phone }}</span>
        </template>
      </el-table-column>
      <el-table-column label="星级" align="center" prop="rating">
        <template #default="scope">
          <dict-tag :options="sys_review_star" :value="scope.row.rating"/>
        </template>
      </el-table-column>
      <el-table-column label="图片" align="center" prop="images" width="100">
        <template #default="scope">
          <image-preview :src="scope.row.images" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column min-width="200" label="评价内容" align="center" prop="content"/>
      <!--<el-table-column label="服务评价ID" align="center" prop="reviewId" />-->
      <el-table-column label="评价时间" align="center" prop="reviewTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.reviewTime, '{y}-{m}-{d} {h}:{m}:{s}') }}</span>
        </template>
      </el-table-column>
      <!--<el-table-column label="操作" align="center" class-name="small-padding fixed-width">-->
      <!--  <template #default="scope">-->
      <!--    <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['orderManagement:reviewManagement:edit']">修改</el-button>-->
      <!--    <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['orderManagement:reviewManagement:remove']">删除</el-button>-->
      <!--  </template>-->
      <!--</el-table-column>-->
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 添加或修改评价管理对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="reviewManagementRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="评价时间" prop="reviewTime">
          <el-date-picker clearable
                          v-model="form.reviewTime"
                          type="date"
                          value-format="YYYY-MM-DD"
                          placeholder="请选择评价时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="评价内容" prop="content">
          <el-input v-model="form.content" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="评价图片" prop="images">
          <image-upload v-model="form.images"/>
        </el-form-item>
        <el-form-item label="评价等级" prop="rating">
          <el-select v-model="form.rating" placeholder="请选择评价等级">
            <el-option
                v-for="dict in sys_review_star"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="订单ID" prop="orderId">
          <el-input v-model="form.orderId" placeholder="请输入订单ID"/>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="ReviewManagement">
import {
  addReviewManagement,
  delReviewManagement,
  getReviewManagement,
  listReviewManagement,
  updateReviewManagement
} from "@/api/orderManagement/reviewManagement";

const {proxy} = getCurrentInstance();
const {sys_review_star} = proxy.useDict('sys_review_star');

const reviewManagementList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const daterangeReviewTime = ref([]);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    reviewTime: null,
    rating: null,
    orderId: null,
    serviceName: null
  },
  rules: {}
});

const {queryParams, form, rules} = toRefs(data);

// 修改的地方：处理后的数据
const processedReviewManagementList = computed(() => {
  return reviewManagementList.value.flatMap(review => {
    return review.serviceItem.map(service => {
      return {
        ...review,
        serviceName: service.serviceName
      };
    });
  });
});

/** 查询评价管理列表 */
function getList() {
  loading.value = true;
  // queryParams.value.params = {};
  if (null != daterangeReviewTime && '' != daterangeReviewTime) {
    queryParams.value.beginReviewTime = daterangeReviewTime.value[0];
    queryParams.value.endReviewTime = daterangeReviewTime.value[1];
  }
  listReviewManagement(queryParams.value).then(response => {
    console.log(queryParams.value);
    reviewManagementList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    reviewId: null,
    reviewTime: null,
    content: null,
    images: null,
    rating: null,
    memberId: null,
    orderId: null,
    ext1: null,
    ext2: null,
    ext3: null,
    ext4: null,
    ext5: null,
    serviceName: null
  };
  proxy.resetForm("reviewManagementRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  // 确保 serviceItem 是一个数组
  // if (!Array.isArray(queryParams.value.serviceItem)) {
  //   queryParams.value.serviceItem = [queryParams.value.serviceItem];
  // }
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  daterangeReviewTime.value = [];
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.reviewId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加评价管理";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _reviewId = row.reviewId || ids.value
  getReviewManagement(_reviewId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改评价管理";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["reviewManagementRef"].validate(valid => {
    if (valid) {
      if (form.value.reviewId != null) {
        updateReviewManagement(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addReviewManagement(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _reviewIds = row.reviewId || ids.value;
  proxy.$modal.confirm('是否确认删除评价管理编号为"' + _reviewIds + '"的数据项？').then(function () {
    return delReviewManagement(_reviewIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {
  });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('orderManagement/reviewManagement/export', {
    ...queryParams.value
  }, `reviewManagement_${new Date().getTime()}.xlsx`)
}

getList();
</script>